#!/bin/bash
# DNSupdater, make sure that you encode the @ as %40
USERNAME=login
PASSWORD=pw
HOST=host
LOGFILE=/var/log/ddns/ddns.log
STOREDIPFILE=/var/log/ddns/current_ip
USERAGENT="Simple Bash DDNS ip Updater/0.4 admin@engelsoft.com.br"
if [ ! -e $STOREDIPFILE ]; then
  touch $STOREDIPFILE
fi
NEWIP=$(wget -O - https://ddns.egsf.tk/update/ip -o /dev/null --no-check-certificate)
echo "Ip atual $NEWIP"

STOREDIP=$(cat $STOREDIPFILE)
if [ "$NEWIP" != "$STOREDIP" ]; then
  echo "Mudanca de ip detectada"
  RESULT=$(wget -O "$LOGFILE" -q --user-agent="$USERAGENT" --no-check-certificate "https://$USERNAME:$PASSWORD@ddns.egsf.tk/update/host/$HOST/$NEWIP")
  LOGLINE="[$(date +"%Y-%m-%d %H:%M:%S")] $RESULT"
  echo $NEWIP > $STOREDIPFILE
 else
  echo "Nao houve mudanca de ip"
  LOGLINE="[$(date +"%Y-%m-%d %H:%M:%S")] No IP change"
fi
echo "Fim do script"
echo $LOGLINE >> $LOGFILE
exit 0
